package com.plm.frame.action;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.plm.frame.model.*;
import com.plm.frame.service.DingdanService;
import com.plm.frame.service.FavoriteService;
import com.plm.frame.service.FenleiService;
import com.plm.frame.service.ProductService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.plm.frame.core.Constants;

@Controller
public class ProductAction extends BaseAction{

	@Resource
	private ProductService productService;
	
	
	@Resource
	private FavoriteService favoriteService;
	
	@Resource
	private FenleiService fenleiService;
	
	@Resource
	private DingdanService dingdanService;
	
	@RequestMapping("/products")
	public String product(Map m,Product product){
		//查询是否喜欢
		Favorite favorite = new Favorite();
		favorite.setUsername(((User) getSession().getAttribute(Constants.LOGIN_USER)).getUsername());
		favorite.setCid(product.getId());	
		//1喜欢  0未喜欢
		if (favoriteService.selectFavorite(favorite).size()>0&&favoriteService.selectFavorite(favorite)!=null)
			m.put("fcode", 1);			
		else
			m.put("fcode", 0);
		m.put("fl", fenleiService.selectFenleiAll());
		m.put("pd", productService.selectProduct(product));
		return "juhuasuanlist";
	}

	@RequestMapping("/selectProduct")
	public String selectProduct(Map m,Product product){
		//查询是否喜欢
		Favorite favorite = new Favorite();
		favorite.setUsername(((User) getSession().getAttribute(Constants.LOGIN_USER)).getUsername());
		favorite.setCid(product.getId());
		//1喜欢  0未喜欢
		if (favoriteService.selectFavorite(favorite).size()>0&&favoriteService.selectFavorite(favorite)!=null)
			m.put("fcode", 1);
		else
			m.put("fcode", 0);
		m.put("fl", fenleiService.selectFenleiAll());
		m.put("pd", productService.selectProduct(product));
		return "juhuasuanlist";
	}
		
	@RequestMapping("/showProduct")
	public String showProduct(Map m,Product product){
			//查询是否喜欢
			Favorite favorite = new Favorite();
			favorite.setUsername(((User) getSession().getAttribute(Constants.LOGIN_USER)).getUsername());
			favorite.setCid(product.getId());	
			//1喜欢  0未喜欢
			if (favoriteService.selectFavorite(favorite).size()>0&&favoriteService.selectFavorite(favorite)!=null)
				m.put("fcode", 1);			
			else
				m.put("fcode", 0);
			m.put("pd", productService.selectProductAsId(product));
			return "showjuhuasuan";	
	}
	
	@RequestMapping("/addProduct")
	public String addProduct(Map<String,List> m){
		return "addproduct";
	}
	
	@RequestMapping("/updateProduct")
	public String updateProduct(Product product,HttpSession session){
		Product updateProdect = (Product) session.getAttribute("updateProdect");
		product.setId(updateProdect.getId());
		productService.editProduct(product);
		session.removeAttribute("updateProdect");
		return "redirect:adminproduct";
	}

	@RequestMapping("/addproductVerification")
	public String addproductVerification(@RequestParam("productphoto") MultipartFile file,
			HttpServletRequest request,
			Product product,
			Map<String,List> m) throws IllegalStateException, IOException{
		// 项目在容器中实际发布运行的根路径
		String realPath=request.getSession().getServletContext().getRealPath("/");
		//新的文件名
		String fileName = new Date().getTime()+".jpg";
		//保存路径
		String path=realPath+"uploadimg\\"+fileName;
		// 转存文件到指定的路径
		System.out.println(path);
		file.transferTo(new File(path));
		//写入数据库
		product.setImage("uploadimg/"+fileName);
		productService.addProduct(product);
		return "redirect:adminindex";
	}
	
	@RequestMapping("/deleteProduct")
	public String deleteProduct(Map<String,List> m,Product product){
		productService.deleteProduct(product);
		//删除产品附带收藏
		Favorite favorite = new Favorite();
		favorite.setCid(product.getId());
		favoriteService.deleteFavorite(favorite);
		//删除对应订单
		Dingdan dingdan = new Dingdan();
		dingdan.setDid(product.getId());
		dingdanService.deleteDingdan(dingdan);
		return "redirect:adminproduct";
	}

	@RequestMapping("/toUpdateProduct")
	public String toUpdateProduct(HttpSession session, Product product){
		session.setAttribute("updateProdect",productService.selectProductAsId(product));
		session.setAttribute("FL",fenleiService.selectFenleiAll());
		return "adminupdate";
	}

	@RequestMapping("/editProduct")
	public String editProduct(Map m,Product product){
		m.put("pd", productService.selectProductAsId(product));
		m.put("fl", fenleiService.selectFenleiAll());
		return "editproduct";
	}
	
	@RequestMapping("/editProductVerification")
	public String editProductVerification(Map m,
			Product product,
			HttpServletRequest request){
		productService.editProduct(product);
		return "redirect:editProduct?id="+product.getId();
	}
	
	
}
